package com.cell.controller;


import com.cell.common.Result;
import com.cell.common.States;
import com.cell.pojo.DTO.user.loginaccount.*;
import com.cell.pojo.DTO.user.validation.validateCodeDTO;
import com.cell.service.user.IUserLoginAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cloudJiang
 * @since 2022-06-04
 */
@RestController
@RequestMapping("/userLoginAccount")
@Api(tags = "用户登录以及登录相关信息修改接口")
public class UserLoginAccountController {
    @Autowired
    IUserLoginAccountService loginAccountService;


    /**
     * 通过密码登录
     *
     * @return
     */
    @ApiOperation(value = "密码登录", notes = "通过调用loginByPassword方法，使用手机号或者邮箱配合密码进行登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginKey", value = "用户登录的手机号或邮箱", required = true),
            @ApiImplicitParam(name = "password", value = "登录密码", required = true),
            @ApiImplicitParam(name = "loginByPasswordDTO", value = "以上属性被封装到这个DTO里")
    })
    @PostMapping("/loginByPassword")
    Result loginByPassword(@Validated @RequestBody loginByPasswordDTO loginByPasswordDTO, HttpServletRequest request) {
        request.getSession().setAttribute("user", loginAccountService.loginByPassword(loginByPasswordDTO));
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 在修改密码前进行旧密码比对
     *
     * @return
     */
    @ApiOperation(value = "验证旧密码", notes = "通过调用validOldPassword方法，使用用户ID来验证旧密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "int", required = true),
            @ApiImplicitParam(name = "password", value = "旧密码", required = true),
            @ApiImplicitParam(name = "validOldPasswordDTO", value = "以上两个参数封装在此DTO中")
    })
    @PostMapping("/validOldPassword")
    Result validOldPassword(@Validated @RequestBody validOldPasswordDTO validOldPasswordDTO) {
        loginAccountService.validOldPassword(validOldPasswordDTO);
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 通过验证码登录
     *
     * @return
     */
    @ApiOperation(value = "验证码登录", notes = "通过调用loginByValidCode方法，使用手机号或者邮箱配合验证码进行登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginKey", value = "用户登录的手机号或邮箱", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", required = true),
            @ApiImplicitParam(name = "loginByValidCodeDTO", value = "以上属性被封装到这个DTO里")
    })
    @PostMapping("/loginByValidCode")
    Result loginByValidCode(@Validated @RequestBody validateCodeDTO validateCodeDTO, HttpServletRequest request) {
        request.getSession().setAttribute("user", loginAccountService.loginByValidCode(validateCodeDTO));
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 重置用户密码
     *
     * @return
     */
    @ApiOperation(value = "重置用户密码", notes = "通过调用resetPassword方法使用password和loginAccountId两个参数来重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "password", value = "用户密码", required = true),
            @ApiImplicitParam(name = "loginAccountId", value = "用户的登录账户ID", required = true),
            @ApiImplicitParam(name = "ResetPasswordDTO", value = "以上两个参数封装在此DTO中")
    })
    @PostMapping("/resetPassword")
    Result resetPassword(@Validated @RequestBody ResetPasswordDTO resetPasswordDTO) {
        loginAccountService.resetPassword(resetPasswordDTO.getPassword(), resetPasswordDTO.getLoginAccountId());
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 修改用户手机号
     *
     * @return
     */
    @ApiOperation(value = "修改用户手机号", notes = "通过调用resetPhone方法，使用accountId以及phone两个参数来重置手机号码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountId", value = "用户的账户ID", paramType = "Long", required = true),
            @ApiImplicitParam(name = "phone", value = "用户手机号", required = true),
            @ApiImplicitParam(name = "resetPhoneDTO", value = "以上两个参数封装在此DTO中")
    })
    @PostMapping("/resetPhone")
    Result resetPhone(@Validated @RequestBody resetPhoneDTO phoneDTO) {
        loginAccountService.resetPhone(phoneDTO.getAccountId(), phoneDTO.getPhone());
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 修改用户邮箱
     *
     * @return
     */
    @ApiOperation(value = "修改用户邮箱", notes = "通过调用resetEmail方法，使用accountId以及email两个参数来重置手机号码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountId", value = "用户的账户ID", paramType = "Long", required = true),
            @ApiImplicitParam(name = "email", value = "用户邮箱", required = true),
            @ApiImplicitParam(name = "resetEmailDTO", value = "以上两个参数封装在此DTO中")
    })
    @PostMapping("/resetEmail")
    Result resetEmail(@Validated @RequestBody resetEmailDTO emailDTO) {
        loginAccountService.resetEmail(emailDTO.getAccountId(), emailDTO.getEmail());
        return new Result(null, States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 获取用户手机号 不进行脱敏处理
     *
     * @return
     */
    @ApiOperation(value = "获取用户手机号，不脱敏", notes = "通过调用getTurePhone方法，使用accountId数来获取用户手机号码(不进行脱敏处理)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountId", value = "用户的账户ID", paramType = "Long", required = true)
    })
    @GetMapping("/getTurePhone")
    Result getTurePhone(Long accountId) {
        return new Result(loginAccountService.getTurePhone(accountId),
                States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 获取用户手机号 进行脱敏处理（返回的字符串中间四位使用*号代替)
     *
     * @return
     */
    @ApiOperation(value = "获取用户手机号，脱敏", notes = "通过调用getPhone方法，使用accountId数来获取用户手机号码(进行脱敏处理)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountId", value = "用户的账户ID", paramType = "Long", required = true)
    })
    @GetMapping("/getPhone")
    Result getPhone(Long accountId) {
        return new Result(loginAccountService.getPhone(accountId),
                States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 获取用户邮箱
     *
     * @return
     */
    @ApiOperation(value = "获取用户邮箱", notes = "通过调用getEmail方法，使用accountId数来获取用户邮箱")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accountId", value = "用户的账户ID", paramType = "Long", required = true)
    })
    @GetMapping("/getEmail")
    Result getEmail(Long accountId) {
        return new Result(loginAccountService.getEmail(accountId),
                States.OK.getStates(), States.OK.getDescription());
    }
}

